/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.util.benchmark;

class FunctionBenchmark implements Benchmark {

	var f:Void->Dynamic;
	var callOnFinish:String->Int->Void;
	var iterations:Int;
	var name:String;
	
	public function new(name:String, functionToTest:Void->Dynamic, iterations:Int, ?callOnFinish:String->Int->Void=null) {
		this.name = name;
		this.iterations = iterations;
		this.callOnFinish = callOnFinish==null ? traceResult : callOnFinish;
		this.f = functionToTest;
	}
	
	public function run():Void {
		Stoper.start();
		var i = iterations;
		while(i-- > 0) f();
		if (callOnFinish != null) callOnFinish(name, Stoper.millis());
	}
	
	function traceResult(name:String, millis:Int):Void {
		trace(name + " : " + millis);
	}
}